package com.max.xihu.netty.nio.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @Author: Max
 * @Date: 2020-03-08 21:21
 * @Description:
 */
public class HelloServer {

    public static void main(String[] args) throws Exception{

        /**
         * 定义一对线程组
         */

        //定义主线程组，负责处理客户端连接
        EventLoopGroup parentGroup = new NioEventLoopGroup();

        //定义从线程组，负责处理主线程组丢过来的任务
        EventLoopGroup childGroup = new NioEventLoopGroup();

        try {
            //netty服务的启动类
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(parentGroup,childGroup)
                    .channel(NioServerSocketChannel.class) //注册主从线程组
                    .childHandler(new HelloServerInitializer()); //设置子处理器，用于处理workerGroup

            // 启动server，同时设置启动端口为8088，启动方式为同步
            ChannelFuture channelFuture = serverBootstrap.bind(8088).sync();
            //监听关闭channel，设置为同步方式
            channelFuture.channel().closeFuture().sync();
        }finally {
            childGroup.shutdownGracefully();
            parentGroup.shutdownGracefully();
        }

    }
}
